/*
 *  DropdownList.cpp
 *  Fridge
 *
 *  Created by Jeremy Abel on 7/1/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "../Main.h"

#include "DropdownList.h"

#include "../Controller/Controller_Main.h"
#include "../Input.h"
#include "../InputKeys.h"
#include "../TextRender.h"
#include "../TextureManager.h"
#include "DialogPanel.h"

const u_int uDROPDOWN_TEXT_LIMIT = 64;
const u_int uDROPDOWN_OPEN_ENTRY_WIDTH = 192;
const u_int uDROPDOWN_OPEN_ENTRY_HEIGHT = 24;
const u_int uDROPDOWN_ICON_MARGIN = 2;

DialogPanel*	DropdownList::s_pxOpenDropdown = 0;

class DD_ListEntry
{
	public:
		
		char m_szName[ 64 ];
		ButtonPressCallback m_pfnClickCallback;
		void* m_pButtonPressUserData;
		DropdownList* m_pxDropdownList;
};

DropdownList::DropdownList( Widget* pxParent ) : Button( pxParent )
{
	m_bCloseOpenListNextFrame = false;
	m_pxDropdownEntries = new TypedList< DD_ListEntry >( true );
	m_pfnCallback = DropdownTitleClickCallback;
	m_pCallbackUserData = this;
	m_bCentreAlignText = false;
}

DropdownList::~DropdownList()
{
	CloseOpenList();
}

void DropdownList::Render()
{
	Button::Render();

	if( !s_pxOpenDropdown && MouseInWidget() )
	{
		TextureManager* pxTextureManager = Controller_Main::GetTextureManager();
		
		if( pxTextureManager )
		{
			const int iTextureID = pxTextureManager->GetTextureID( GetStringHash( "Expand32" ) );
		
			const u_int uIconY1 = GetResizedY() + uDROPDOWN_ICON_MARGIN;
			const u_int uIconY2 = GetResizedY() + GetResizedHeight() - uDROPDOWN_ICON_MARGIN;
		
			const u_int uIconX2 = GetResizedX() + GetResizedWidth() - uDROPDOWN_ICON_MARGIN;
			const u_int uIconX1 = uIconX2 - ( uIconY2 - uIconY1 );

			glColor4f( 1.0f, 1.0f, 1.0f, 0.3f );
			glBindTexture( GL_TEXTURE_2D, iTextureID );

			glBegin( GL_QUADS );

			glTexCoord2f( 0.0f, 0.0f );
			glVertex2i( uIconX1, uIconY1 );
			glTexCoord2f( 0.0f, 1.0f );
			glVertex2i( uIconX1, uIconY2 );
			glTexCoord2f( 1.0f, 1.0f );
			glVertex2i( uIconX2, uIconY2 );
			glTexCoord2f( 1.0f, 0.0f );
			glVertex2i( uIconX2, uIconY1 );

			glEnd();
		}
	}
}

void DropdownList::Update()
{	
	if( m_bCloseOpenListNextFrame )
	{
		CloseOpenList();
		m_bCloseOpenListNextFrame = false;
	}
	
	if( s_pxOpenDropdown )
	{
		s_pxOpenDropdown->Update();
		
		const Input* pxInput = Controller_Main::GetInputHandler();
		
		if( ( ( pxInput->MouseButtonPressed( INPUT_MOUSE_BUTTON_LEFT ) ||
				pxInput->MouseButtonPressed( INPUT_MOUSE_BUTTON_RIGHT ) ||
				pxInput->MouseButtonPressed( INPUT_MOUSE_BUTTON_MIDDLE ) ) &&
				!s_pxOpenDropdown->MouseInWidget() ) ||
				pxInput->KeyPressed( INPUT_KEY_ESC ) )
		{
			CloseOpenList();
		}
	}
	
	Button::Update();
}

void DropdownList::AddListItem( const char* szName, ButtonPressCallback pfnCallback, void* pUserData )
{	
	DD_ListEntry* pxListEntry = new DD_ListEntry();
	pxListEntry->m_pfnClickCallback = pfnCallback;
	pxListEntry->m_pButtonPressUserData = pUserData;
	strncpy( pxListEntry->m_szName, szName, uDROPDOWN_TEXT_LIMIT );
	pxListEntry->m_pxDropdownList = this;
	m_pxDropdownEntries->Add( pxListEntry );
	
	// Set button label to the first entry
	if( m_pxDropdownEntries->Count() == 1 )
	{
		SetLabel( szName );
	}
}

void DropdownList::DropdownTitleClickCallback( void* pData )
{	
	DropdownList* pxDropdownList = reinterpret_cast< DropdownList* > ( pData );
	
	Widget* pxScreen = pxDropdownList->GetParent();
	
	s_pxOpenDropdown = new DialogPanel( pxScreen );
	s_pxOpenDropdown->SetPosition( pxDropdownList->GetResizedX(), pxDropdownList->GetResizedY() + pxDropdownList->GetResizedHeight() );
	s_pxOpenDropdown->SetAnchors( ANCHOR_TOP | ANCHOR_LEFT );
	s_pxOpenDropdown->SetHasBorder( true );
	
	int iPos = 8;
	
	for( int iDropdownEntry = 0; iDropdownEntry < (int)pxDropdownList->m_pxDropdownEntries->Count(); ++iDropdownEntry )
	{
		DD_ListEntry* pxDropdownEntry = pxDropdownList->m_pxDropdownEntries->Get( iDropdownEntry );
		
		Button* pxButton = new Button( s_pxOpenDropdown );
		pxButton->SetLabel( pxDropdownEntry->m_szName );
		pxButton->SetButtonPressCallback( DropdownItemClickCallback, pxDropdownEntry );
		pxButton->SetPosition( s_pxOpenDropdown->GetOriginalX(), s_pxOpenDropdown->GetOriginalY() + iPos );
		pxButton->SetSize( uDROPDOWN_OPEN_ENTRY_WIDTH, uDROPDOWN_OPEN_ENTRY_HEIGHT );
		pxButton->SetFlatStyle( true );
		pxButton->SetCentreAlignText( false );
		
		iPos += uDROPDOWN_OPEN_ENTRY_HEIGHT;
	}
	
	s_pxOpenDropdown->SetSize( uDROPDOWN_OPEN_ENTRY_WIDTH, iPos + 8 );
	
	Controller_Main::AddModal( pxDropdownList );
}

void DropdownList::DropdownItemClickCallback( void* pData )
{
	DD_ListEntry* pxDropdownEntry = reinterpret_cast< DD_ListEntry* > ( pData );
	
	FAssert( pxDropdownEntry != 0 );
	if( !pxDropdownEntry )
	{
		return;
	}
	
	// Call callback
	if( pxDropdownEntry->m_pfnClickCallback )
	{
		pxDropdownEntry->m_pfnClickCallback( pxDropdownEntry->m_pButtonPressUserData );
	}
	
	// Set title text to the name of the selection
	pxDropdownEntry->m_pxDropdownList->SetLabel( pxDropdownEntry->m_szName );
	
	// Close the open list
	pxDropdownEntry->m_pxDropdownList->m_bCloseOpenListNextFrame = true;
}

void DropdownList::CloseOpenList()
{
	FridgeLog("CLOSE OPEN LIST");
	delete s_pxOpenDropdown;
	s_pxOpenDropdown = 0;
	
	Controller_Main::RemoveModal( this );
}

// eof
